package com.blog.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author cgx
 * @Date 2021/9/3 10:37
 */
public class DateFormatUtil {
    /**
     *时间转换类，date转换为String   yyyy-MM-dd
     * @param date
     * @return
     */
    public static String dataToStr(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }
    /**
     *时间转换类，date转换为String   yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static String dataToStrLong(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }


    /**
     * 获取 23点59分59秒的时间
     * @param sign -1昨天，-2前天,以此类推
     * @return
     * @example sign = -1，今天是 2021-11-11，返回时间 2021-11-10 23:59:59
     */
    public static Date getYesterdayDate(Integer sign) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,sign);
        String aaa = " 23:59:59";
        String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
        String bbb = yesterday+aaa;
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(bbb);
    }
}
